/*
 * Created by SharpDevelop.
 * User: Edward Wilde (http://blogs.charteris.com/edwardw)
 * Date: 02/07/2007
 * Time: 06:46
 * 
 */

using System;
using System.Collections.Generic;
using WatiN.Core;
using WatiN.Core.DialogHandlers;

namespace Wax
{
	/// <summary>
	/// Defines the execution context used when executing <see cref="Test">Tests</see> and
	/// <see cref="Command">Commands</see>.
	/// </summary>
	public class ExecutionContext : IExecutionContext
	{
		
		#region Private fields

        /// <summary>
        /// Reference to the test mananer running the tests.
        /// </summary>
		private readonly ITestManager testManager;

        /// <summary>
        /// Stores a dictionary of dialog handlers that are registered and used by test <see cref="DialogHandlerCommand">commands</see>
        /// </summary>
        private readonly System.Collections.Generic.Dictionary<string, IDialogHandler> dialogHandlers;

        /// <summary>
        /// The last found element.
        /// </summary>
	    private Element lastFoundElement;

        /// <summary>
        /// The last found value.
        /// </summary>
	    private string lastFoundValue;
	    
	    #endregion
		
		#region Constructors
		
		/// <summary>
		/// Creates a new instance of the <see cref="ExecutionContext" /> class.
		/// </summary>
		[CLSCompliant(false)]
		public ExecutionContext(ITestManager testManager)
		{
			this.testManager = testManager;
            this.dialogHandlers = new Dictionary<string, IDialogHandler>();
		}
		
		#endregion
		
		#region Public properties
		
		/// <summary>
		/// The <see cref="WatiN.Core.IE">browser</see> used during test execution
		/// </summary>
		[CLSCompliant(false)]
		public WatiN.Core.IE Browser
		{
			get
			{
			    return this.testManager.Browser;
			}

            set
            {
                this.testManager.Browser = value;
            }
		}

        /// <summary>
        /// Gets or sets the last found element.
        /// </summary>
        /// <value>The last found element.</value>
        [CLSCompliant(false)]
        public Element LastFoundElement
	    {
            get { return this.lastFoundElement; }
	        set { this.lastFoundElement = value; }
	    }

        /// <summary>
        /// Gets or sets the last found value.
        /// </summary>
        /// <value>The last found value.</value>
	    public string LastFoundValue
	    {
            get { return this.lastFoundValue; }
            set { this.lastFoundValue = value; }
	    }

        /// <summary>
        /// Adds the dialog handler.
        /// </summary>
        /// <param name="dialogId">The dialog id.</param>
        /// <param name="dialogHandler">The dialog handler.</param>
        [CLSCompliant(false)]
        public void AddDialogHandler(string dialogId, IDialogHandler dialogHandler)
	    {
            this.Browser.DialogWatcher.Add(dialogHandler);
	        this.dialogHandlers.Add(dialogId, dialogHandler);
	    }

        /// <summary>
        /// Gets the dialog handler for the specified id,
        /// that was previously <see cref="AddDialogHandler"/>.
        /// </summary>
        /// <param name="dialogId">The dialog id.</param>
        /// <returns>The dialog handler, or null if none exists for the given id.</returns>
        [CLSCompliant(false)]
        public IDialogHandler GetDialogHandler(string dialogId)
	    {
            IDialogHandler dialogHandler = null;
	        if (this.dialogHandlers.ContainsKey(dialogId))
	        {
	            dialogHandler = this.dialogHandlers[dialogId];
	        }

            return dialogHandler;
	    }

	    #endregion
	}
}
